<?php
namespace Home\Controller;
/**
 * 域名提交
 * @author Administrator
 *
 */
class DomainAuditController extends HomeBaseController
{
    public function index(){
        $page=I('page',1);
        $size=10;
        $start=($page-1)*$size;
        
        $UserDomains = M('UserDomains');
        $DomainType = M('DomainType');
        $this->assign('categoryId', '');
        $this->assign('checkstatus', '');
        $condition['a.uid']=session('uid');
        $categoryId=I('categoryId','');
        if(!empty($categoryId) && $categoryId!=''){
            $condition['a.domain_typeid']=$categoryId;
            $this->assign('categoryId', $categoryId);
        }
        $checkstatus=I('checkstatus','');
        if(!empty($checkstatus) && $checkstatus!=''){
            $condition['a.check_status']=$checkstatus;
            $this->assign('checkstatus', $checkstatus);
        }
        $list = $UserDomains->join('as a left join dd_domain_type as b on a.domain_typeid = b.id')
        ->field('a.id as id,a.domain as domain,a.domain_typeid as domain_typeid,a.check_status as check_status
            ,a.trans_status as trans_status,a.is_mortage as is_mortage,a.user_remark as user_remark,
            a.is_lend as is_lend,a.admin_remark as admin_remark,a.regi_buines_name as buisname,
            a.pushId as pushid,a.createdate as createdate,a.check_result as check_result,b.name as name')
        ->where($condition)->order('a.createdate desc' )->limit($start,$size)->select();
        
        $count =$UserDomains->join('as a left join dd_domain_type as b on a.domain_typeid = b.id')->where($condition)->count();
        $this->assign('page', $page);
        $this->assign('pageNum', $count%$size);
        $this->assign('count', $count);
        $this->assign('list', $list);
        $domTypes = $DomainType->select();
        $this->assign('domTypes', $domTypes);
        $this->display('domain_audit/index');
    }
    
    public function detail(){
        $id=I('id',0);
        $obj = M('UserDomains')->where('id='.$id)->find();
        $this->assign('obj', $obj);
        $dtype = M('DomainType')->find($obj->domain_typeid);
        $this->assign('dtype', $dtype);
        $this->display('domain_audit/detail');
    }
    
    public function regverifyCode(){
        $con=array('useNoise' => false,'length'=>4,'useCurve'=>false);
        $Verify = new \Think\Verify($con);
        $Verify->expire=120;//2分钟
        $Verify->fontSize=16;
        $Verify->entry(88898);
    }
    
    public function save(){
        $DomainType = M('DomainType');
        $domTypes = $DomainType->select();
        $this->assign('domTypes', $domTypes);
        
        $condition['a.uid']=session("uid");
        $UserRegbusiness = M('UserRegbusiness');
        $list = $UserRegbusiness->join('as a left join dd_register_business as b on a.businesid = b.id')
        ->field('a.busicode as busicode,b.name as name')
                    ->where($condition)->select();
        $this->assign('list', $list);
        $this->display('domain_audit/save');
    }
    
    public function insert(){
        $category_id=I('category_id',0);//域名类型
        $refix=M('DomainType')->field('refix')->find($category_id);
        if(empty($refix)|| $refix['refix']=='') {
            $this->ajaxReturn(array('status'=>'2','msg' => '域名类型不存在！'));//0错误，1成功
            exit;
        }
        $domains=I('domains','');
        $domain_registrarId=I('domain_registrar','');
        $remark_user=I('remark_user','');
      
        $condi['busicode']=$domain_registrarId;
        $condi['uid']=session("uid");
        
        $UserRegbusiness = M('UserRegbusiness');
        $reg=$UserRegbusiness->where($condi)->select();
        if(empty($reg)){
            $this->ajaxReturn(array('status'=>'2','msg' => $UserRegbusiness->getLastSql()));//0错误，1成功
            exit;
        }
        
        $domainArr=preg_split('/\s+/', $domains);
        if(count((array)$domainArr)>600 || count((array)$domainArr)==0){
            $this->ajaxReturn(array('status'=>'2','msg'=>'提交的域名数量请保持在600个以内！'));//0错误，1成功
            exit();
        }
        
        $data['checkid']=null;
        $RegisterBusiness = M('RegisterBusiness');
        $busname=$RegisterBusiness->where("id=1")->select();
        $sonStr=strtolower($refix['refix']);//将后缀转小写
        $data['uid'] = session('uid');
        $data['domain_typeid'] = $category_id;
        $data['check_status'] = 1;
        $data['user_remark'] = $remark_user;
        $data['is_lend'] = 1;
        $data['regi_buines_name'] = $busname[0]['name'];
        $data['pushId']=$domain_registrarId;
        $data['createdate']=date('Y-m-d H:m:s');
        $data['reg_busin_id']=$category_id;
        $data['checkid']= strtoupper(md5(uniqid(rand(), true)));
       
        $UserDomains = M('UserDomains');
        $i=0;
        $errorDoms='';
        $j=0;
        foreach ($domainArr as $v){
            $v=strtolower($v);
            if(empty($v) || $v=='') {
                continue;
            }
            $zho=substr($v, strlen($v)-strlen($sonStr), strlen($v));
            if($zho!=$sonStr || $v==$sonStr){
                $i=$i+1;
                $errorDoms=',域名格式不正确:'.$v.$errorDoms;
                continue;
            }
                $rst4=null;
                $data['domain']=null;
                $data['expriedate']=null;
                $oldcount=0;
                // 创建数据后写入到数据库
                $data['domain'] = $v;
                $oldcount=$UserDomains->where("domain='".$data['domain']."'")->count();
                if($oldcount>0){
                    $i=$i+1;
                    $errorDoms=',域名已存在:'.$v.$errorDoms;
                    continue;
                }//域名已经存在，直接跳过此域名
                $httph =curl_init('http://whois.chinaz.com/'.$data['domain']);
                curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
                curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);
                curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
                curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($httph, CURLOPT_HEADER,1);
                $rst=curl_exec($httph);
                curl_close($httph);
                if(strpos($rst,"过期时间")>200){
                    $begin=strpos($rst,"过期时间")+55;
                    $end=$begin+30;
                    $rst4= substr($rst,$begin,20);
                    $rst4=str_replace(' ','',$rst4);
                    $rst4=str_replace('年','-',$rst4);
                    $rst4=str_replace('月','-',$rst4);
                    $rst4=str_replace('日','',$rst4);
                    $data['expriedate']=$rst4;
                }
                $UserDomains->data($data)->add();
                $j=$j+1;
        }
        if($i>0){
            $this->ajaxReturn(array('status'=>'4',
                'msg'=>'添加成功'.$j.'个,等待审核！但其中有'.$i.'个域名无法提交！',
                'errdomlist'=>$errorDoms
            ));//0错误，1成功
        }else {
            $this->ajaxReturn(array('status'=>'1','msg'=>'添加成功,等待审核！'));//0错误，1成功
        }
        
    }
    
    public function ddde(){
        $sonStr='.com';
        $v='lewd.com.cn';
        $zho=substr($v, strlen($v)-strlen($sonStr), strlen($v));
        if($zho==$sonStr){
            echo '-------------'.$zho;
        }else {
            echo '-------------';
        }
    }
}